package org.dairy.farms.persistence;

import org.dairy.farms.model.*;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml

            AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration().addAnnotatedClass(org.dairy.farms.model.Person.class).configure();
            annotationConfiguration.addAnnotatedClass(Farm.class);
            annotationConfiguration.addAnnotatedClass(Livestock.class);
            annotationConfiguration.addAnnotatedClass(RoleType.class);
            annotationConfiguration.addAnnotatedClass(Lactation.class);
            annotationConfiguration.addAnnotatedClass(Checkup.class);
            annotationConfiguration.addAnnotatedClass(MedicalAttention.class);
            //annotationConfiguration.addAnnotatedClass(MedicalRecord.class);
            annotationConfiguration.addAnnotatedClass(Vaccine.class);
            annotationConfiguration.addAnnotatedClass(CheckupType.class);
            annotationConfiguration.addAnnotatedClass(Expense.class);
            annotationConfiguration.addAnnotatedClass(ExpenseType.class);
            annotationConfiguration.addAnnotatedClass(FarmExpense.class);
            annotationConfiguration.addAnnotatedClass(LivestockExpense.class);
            annotationConfiguration.addAnnotatedClass(LivestockStatusType.class);
            annotationConfiguration.addAnnotatedClass(Feed.class);
            annotationConfiguration.addAnnotatedClass(FeedType.class);
            annotationConfiguration.addAnnotatedClass(FeedExpense.class);
            annotationConfiguration.addAnnotatedClass(CheckupExpense.class);
            annotationConfiguration.addAnnotatedClass(PersonExpense.class);
            annotationConfiguration.addAnnotatedClass(Revenue.class);
            annotationConfiguration.addAnnotatedClass(RevenueType.class);
            annotationConfiguration.addAnnotatedClass(FarmRevenue.class);
            annotationConfiguration.addAnnotatedClass(LivestockRevenue.class);
            annotationConfiguration.addAnnotatedClass(PersonRevenue.class);
            annotationConfiguration.addAnnotatedClass(LactationRevenue.class);
            annotationConfiguration.addAnnotatedClass(VaccineType.class);
            annotationConfiguration.addAnnotatedClass(MedicalAttentionType.class);
            annotationConfiguration.addAnnotatedClass(Notes.class);
            annotationConfiguration.addAnnotatedClass(CheckupNotes.class);
            annotationConfiguration.addAnnotatedClass(ExpenseNotes.class);
            annotationConfiguration.addAnnotatedClass(FeedNotes.class);
            annotationConfiguration.addAnnotatedClass(LactationNotes.class);
            annotationConfiguration.addAnnotatedClass(LivestockNotes.class);
            annotationConfiguration.addAnnotatedClass(PersonNotes.class);
            annotationConfiguration.addAnnotatedClass(RevenueNotes.class);
            annotationConfiguration.addAnnotatedClass(NotesType.class);
            annotationConfiguration.addAnnotatedClass(File.class);
            annotationConfiguration.addAnnotatedClass(LivestockFileType.class);
            annotationConfiguration.addAnnotatedClass(Medicine.class);
            annotationConfiguration.addAnnotatedClass(MedicineType.class);
            annotationConfiguration.addAnnotatedClass(LivestockLifeCycleManager.class);
            new SchemaExport(annotationConfiguration).create(true, true);
            return annotationConfiguration.buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }

}